package com.damn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.NamingException;


public class DBService {
	/**
	 * Function to get Database connection
	 * @param : void
	 * @return : If connection is established, returns Connection object, otherwise returns a null object
	 */	
	public static Connection getConnection(){		
		Connection newConn = null;
		try {
			
			Class.forName("oracle.jdbc.OracleDriver");
			String username = "a59d"; 
			String password = "a59d"; 
			String url = "jdbc:oracle:thin:@localhost:1521:XE";			
			 newConn = (Connection) DriverManager.getConnection(url,username,password);			 
			 if(newConn == null)	{			 			 
			 javax.sql.DataSource ds;				
				ds = (javax.sql.DataSource)new javax.naming.InitialContext().lookup("java:comp/env/jdbc/javaspace");
				newConn = ds.getConnection();				
			 }			 
			 return newConn;
			
		} 
		catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			return null;
		}
		catch (SQLException sqle) {
			sqle.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
					
	}
}
